package com.lixiaoyu.test;

import com.lixiaoyu.controller.ExpressController;
import com.lixiaoyu.controller.UserController;
import com.lixiaoyu.entity.ExpressBoxData;
import com.lixiaoyu.entity.ExpressData;
import com.lixiaoyu.entity.ExpressUser;
import org.junit.Test;

import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

/**
 * 用户端
 */
public class UserProt {

    //用户 控制层
    UserController userController = new UserController();

    //快递操作 控制层
    ExpressController expressController = new ExpressController();

    /**
     * 启动操作
     */
    @Test
    public  void startUp(){

        //启动
        comOnBaby();
    }


    public  void  comOnBaby(){

        Scanner input = new Scanner(System.in);
        System.out.println("欢迎启动【快递E栈】");
        System.out.println("默认账号管理员:100    密码:123456");
        System.out.println("默认账号普通用户:200    密码:123456");
        System.out.println("请选择: 1 -- 登陆    2 -- 注册");

        int i = input.nextInt();
        //登陆
        if( i == 1){
            login();
        //注册
        }else if(i == 2){
            register();
        //输入错误
        }else{
            System.out.println("输入错误，系统重启！");
            comOnBaby();
        }

    }


    //登陆
    public void login(){

        ExpressUser expressUser = new ExpressUser();
        Scanner input = new Scanner(System.in);

        System.out.println("请输入用户名:");
        //输入的账号
        int userName = input.nextInt();
        expressUser.setUserName(userName);

        System.out.println("请输入密码:");
        //输入的密码
        String pussWorld = input.next();
        expressUser.setPussWord(pussWorld);

        //登陆后端     Integer  0 管理员  | 1 用户 | 3 登陆失败
        Map<Integer,String> loginReturn =  userController.loginExpress(expressUser);
        Set<Integer> integers1 = loginReturn.keySet();
        for (Integer key : integers1){
            System.out.println(loginReturn.get(key));
            break;
        }

        if(!"登陆成功".equals(loginReturn.get(1)) && !"登陆成功".equals(loginReturn.get(0))){
            //重新登陆
            login();
        }
        Set<Integer> integers = loginReturn.keySet();
        for(Integer key : integers){
            //校验 是管理员  还是普通用户
            if(key == 0){
                // 管理员登陆
                adminLogin();
            }else if(key == 1){
                // 普通用户 登录
                userLogin();
            }
            //这个循环只会执行一次
            break;
        }
    }

    private void adminLogin() {
        Scanner input = new Scanner(System.in);
        System.out.println("管理员登陆:请选择操作");
        System.out.println("1 --- [快递录入]");
        System.out.println("2 --- [取出快递]");
        System.out.println("3 --- [修改快递]");
        System.out.println("4 --- [查询所有快递]");
        System.out.println("5 --- [系统首页]");

        int admin = input.nextInt();
        if(admin == 1){
            //添加快递
            insertExpress(0);
        }else if(admin == 2){
            //取出快递
            deleteExpress(0);
        }else if(admin == 3){
            //修改快递信息
            updateExpress();
        }else if(admin == 4){
            //查看所有的快递
            selectAllExpress();
        }else if(admin == 5){
            comOnBaby();
        }else{
            System.out.println("输入有误请重新录入！！！");
            adminLogin();
        }
    }

    //修改快递 信息
    private void updateExpress() {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入要修改快递信息的取件码:");
        String code = input.next();
        System.out.println(code.length());
        //获取快递信息
        ExpressBoxData expressBoxDataList = expressController.selectExpress(code);
        if(expressBoxDataList != null ){
            System.out.println("快递信息:取件码["+expressBoxDataList.getTakeCode()+"],快递单号:["+expressBoxDataList.getOrderCode()+"],快递备注:["+expressBoxDataList.getExpressRemarks()+"],手机号:["+expressBoxDataList.getTakePhoneNumber()+"]");
            System.out.println("是否修改快递");
            System.out.println("1 --- 修改");
            System.out.println("输入任意数字返回登陆选项");
            int i = input.nextInt();
            //1 就是取出   输入其他
            if( i == 1){
                yesUpdateExpress(code);
            }else{
                adminLogin();
            }
        }else{
            System.out.println("没有找到此快递");
            adminLogin();
        }

    }

    /**
     * 修改快递信息
     * @param code  取件码
     */
    private void yesUpdateExpress(String code) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入修改的快递单号:");
        String expressCode = input.next();

        System.out.println("请输入修改的快递备注:");
        String expressRemark = input.next();

        System.out.println("请输入修改的快递公司:");
        System.out.println("12 --- 顺丰快递");
        System.out.println("23 --- 圆通快递");
        System.out.println("34 --- 韵达快递");
        System.out.println("45 --- 国通快递");
        System.out.println("56 --- 天天快递");
        System.out.println("67 --- 中通快递");
        System.out.println("78 --- 优速快递");
        System.out.println("89 --- 百世快递");
        System.out.println("90 --- 京东快递");
        String next = input.next();

        System.out.println("请输入修改的取件人手机号:");
        String getExpressPhone = input.next();

        String returnUpdate = expressController.yesUpdateExpress(expressCode,expressRemark,next,getExpressPhone,code);
        System.out.println(returnUpdate);
        adminLogin();
    }

    /**
     * 查询  所有数据层
     */
    private void selectAllExpress() {

        List<ExpressBoxData> expressBoxDataList = expressController.selectAllExpress();
        if(expressBoxDataList.size() == 0){
            System.out.println("快递柜已全部取出，没有快递！");
            adminLogin();
        }
        for (ExpressBoxData expressBoxData:
                expressBoxDataList) {
            System.out.println(expressBoxData.toString());
        }
        adminLogin();
    }

    /**
     *
     * @param a   登陆权限操作， a=0 管理员   a=1 普通用户
     */
    private void insertExpress(int a) {
        Scanner input= new Scanner(System.in);
        ExpressData expressData = new ExpressData();

        System.out.println("请输入备注信息:");
        String expressRemarks = input.next();

        System.out.println("请录入快递单号:");
        String orderCode = input.next();

        System.out.println("请输入对应的快递公司:");
        System.out.println("12 --- 顺丰快递");
        System.out.println("23 --- 圆通快递");
        System.out.println("34 --- 韵达快递");
        System.out.println("45 --- 国通快递");
        System.out.println("56 --- 天天快递");
        System.out.println("67 --- 中通快递");
        System.out.println("78 --- 优速快递");
        System.out.println("89 --- 百世快递");
        System.out.println("90 --- 京东快递");
        int expressCompany = input.nextInt();

        System.out.println("请录入取件人手机号:");
        String takePhoneNumber = input.next();




        expressData.setExpressCompany(expressCompany);
        expressData.setOrderCode(orderCode);
        expressData.setTakePhoneNumber(takePhoneNumber);
        expressData.setExpressRemarks(expressRemarks);
        String returnExpress = expressController.insertExpress(expressData);
        System.out.println(returnExpress);
        adminLogin();

    }

    private void userLogin() {
        ExpressController expressController = new ExpressController();

        Scanner input = new Scanner(System.in);
        System.out.println("普通用户登陆:请选择操作");
        System.out.println("1 --- [取出快递]");
        System.out.println("2 --- [系统首页]");

        int user = input.nextInt();
        if(user == 1){
            //取出快递
            deleteExpress(1);
        }else if(user == 2){
            comOnBaby();
        }else{
            System.out.println("输入有误请重新录入！！！");
            adminLogin();
        }
    }

    private void deleteExpress(int a) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入取件码");
        int getExpressCode = input.nextInt();
        //根据取件码 查询到的所有的信息
        List<ExpressBoxData> selectGetExpress = expressController.selectGetExpress(getExpressCode);
        //集合元素个数 是0  就是没有数据
        if(selectGetExpress.size() == 0){
            System.out.println("没有此快递!");
            System.out.println("是否选择重新录入取件码:");
            System.out.println("     1 --- 是");
            System.out.println("任意数字 --- 返回上一层");

            String number = input.next();
            if(number.equals("1")){
                deleteExpress(a);
            }else{
                //a 是0 的话就是  管理员进行的操作
                if(a == 0)
                    adminLogin();
                userLogin();
            }
        }
            for (ExpressBoxData expressBoxData:
                    selectGetExpress) {
                System.out.println("取件码:["+expressBoxData.getTakeCode()+"]--快递柜号:["+expressBoxData.getBoxCode()+"]--快递单号:["+expressBoxData.getOrderCode()+"]--快递备注:["+expressBoxData.getExpressRemarks()+"]");
            }

            System.out.println("是否取出快递:");
            System.out.println("1 --- 取出快递");
            System.out.println("0 --- 不取");
            int i = input.nextInt();
            //取出快递
            if(i == 1){
                String returnDeleteExpresss = expressController.deleteExpress(getExpressCode);
                System.out.println(returnDeleteExpresss);
                if(a == 0){
                    adminLogin();
                }else{
                    deleteExpress(a);
                }
                //不取
            }else if(i == 0){
                if(a == 0){
                    adminLogin();
                }else{
                    deleteExpress(a);
                }
            }else{
                System.out.println("输入有误请重新输入取件码!");
                deleteExpress(a);
            }

    }

    //注册
    public void register(){
        Scanner input = new Scanner(System.in);
        ExpressUser expressUser = new ExpressUser();

        System.out.println("请输入密码:");
        //密码
        String pussWord = input.next();

        System.out.println("请输入用户姓名:");
        //姓名
        String name = input.next();

        System.out.println("请输入身份证号码:");
        //身份证号
        String idno = input.next();

        System.out.println("请输入家庭住址:");
        //住址
        String homeAddress = input.next();

        System.out.println("请输入用户手机号:");
        //手机号
        String phoneNumber = input.next();
        //手机号 的长度校验
        if(phoneNumber.toCharArray().length != 11){
            System.out.println("手机号位数不对请重新输入:");
            register();
        }

        expressUser.setPussWord(pussWord);
        expressUser.setName(name);
        expressUser.setIdno(idno);
        expressUser.setHomeAddress(homeAddress);
        expressUser.setPhoneNumber(phoneNumber);

        //用户注册
        String returnRegister = userController.registerUser(expressUser);
        System.out.println(returnRegister);
        comOnBaby();
    }

}
